<?php
require_once 'Zend/Db.php';
require_once 'Usuario.class.php';

class UserProvider {

	public static function saveUser(Usuario $user) {
		$registry = Zend_Registry :: getInstance();
		$config = $registry->config;

		if ($user->getId() == 0) {
			$db = Zend_Db :: factory('Mysqli', array (
				'host' => $config->database->host,
				'username' => $config->database->username,
				'password' => $config->database->password,
				'dbname' => $config->database->name
			));

			$db->insert('USUARIO', $user->toDbArray());
		} else {
			throw new Exception('Update User TODAVIA NO SE IMPLEMENTA');
		}
	}

	public static function deleteUser($idUsuario) {
		$registry = Zend_Registry :: getInstance();
		$config = $registry->config;
		$db = Zend_Db :: factory('Mysqli', array (
			'host' => $config->database->host,
			'username' => $config->database->username,
			'password' => $config->database->password,
			'dbname' => $config->database->name
		));

		$result = $db->delete('USUARIO', 'USUARIO_ID = ' . $idUsuario);
	}

	public static function getUsers() {
		$registry = Zend_Registry :: getInstance();
		$config = $registry->config;
		$db = Zend_Db :: factory('Mysqli', array (
			'host' => $config->database->host,
			'username' => $config->database->username,
			'password' => $config->database->password,
			'dbname' => $config->database->name
		));

		$rs = $db->fetchAssoc('SELECT * FROM USUARIO');
		$arrayUsers = array ();
		foreach ($rs as $row) {
			$nUsuario = new Usuario();
			$nUsuario->setId($row['USUARIO_ID']);
			$nUsuario->setApellidoMaterno($row['USUARIO_APELLIDO_MATERNO']);
			$nUsuario->setApellidoPaterno($row['USUARIO_APELLIDO_PATERNO']);
			$nUsuario->setMail($row['USUARIO_MAIL']);
			$nUsuario->setNombre($row['USUARIO_NOMBRE']);
			$nUsuario->setPassword($row['USUARIO_PASSWORD']);
			$nUsuario->setRol($row['ROL_ID']);
			$arrayUsers[] = $nUsuario;
		}
		return $arrayUsers;
	}
}
?>